package com.mzpai.product.service.impl;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mzpai.product.exception.ProductResException;
import com.mzpai.product.exception.ProductResExceptionCode;
import com.mzpai.product.mapper.SystemStoreExtractMapper;
import com.mzpai.product.model.StoreReminder;
import com.mzpai.product.model.SystemStore;
import com.mzpai.product.model.SystemStoreBill;
import com.mzpai.product.model.SystemStoreExtract;
import com.mzpai.product.service.StoreReminderService;
import com.mzpai.product.service.SystemStoreBillService;
import com.mzpai.product.service.SystemStoreExtractService;
import com.mzpai.product.service.SystemStoreService;
import com.mzpai.product.util.ServiceChargeUtil;
import com.mzpai.product.vo.WithdrawalApplicateReqVo;
import com.mzpai.product.vo.WithdrawalApplicateVo;
import com.mzpai.service.impl.BaseServiceImpl;
import com.mzpai.util.ArithUtil;
import com.mzpai.util.DateUtil;
import com.mzpai.util.StringUtils;

@Service
public class SystemStoreExtractServiceImpl extends BaseServiceImpl<SystemStoreExtract> implements SystemStoreExtractService {

	Logger logger = LoggerFactory.getLogger(SystemStoreExtractServiceImpl.class);

	@Autowired 
	private SystemStoreExtractMapper systemStoreExtractMapper;
	
	@Autowired 
	private SystemStoreBillService systemStoreBillService;
	
	@Autowired 
	private SystemStoreService systemStoreService;
	
	@Autowired
	private StoreReminderService storeReminderService;

	/**
	 * 1.插入提现申请 eb_system_store_extract
	 * 2.插入账户流水 eb_system_store_bill
	 * 3.插入提现提醒 eb_store_reminder
	 */
	@Override
	public WithdrawalApplicateVo saveWithdrawalApplicate(WithdrawalApplicateReqVo WithdrawalApplicateVo) {
		
		Integer storeId=WithdrawalApplicateVo.getStoreId();
		String bankCode=WithdrawalApplicateVo.getBankCode();
		String bankName=WithdrawalApplicateVo.getBankName();
		String realName=WithdrawalApplicateVo.getRealName();
		String extractType=WithdrawalApplicateVo.getExtractType();
		
		BigDecimal amount=WithdrawalApplicateVo.getAmount();
		Double extractPrice=amount.doubleValue();
		
		SystemStoreExtract systemStoreExtract=new SystemStoreExtract();
		systemStoreExtract.setStoreId(storeId);
		systemStoreExtract.setRealName(realName);
		systemStoreExtract.setExtractType(extractType);
		systemStoreExtract.setBankCode(bankCode);
		systemStoreExtract.setBankName(bankName);
		systemStoreExtract.setBankAddress(bankName);
		//手续费
		Double serviceChargePrice=ServiceChargeUtil.countServiceCharge(extractPrice);	
		
		BigDecimal serviceChargeDecimal=new BigDecimal(serviceChargePrice);
		systemStoreExtract.setServiceCharge(serviceChargeDecimal);
		
		//提现金额
		systemStoreExtract.setExtractPrice(new BigDecimal(extractPrice));
		
		SystemStore systemStore=systemStoreService.queryByPrimaryId(storeId);
		Double nowMoney=systemStore.getNowMoney();
		
		//剩余可提现金额
		Double storeNowMoney=nowMoney-extractPrice;
		systemStoreExtract.setBalance(new BigDecimal(storeNowMoney));
		//修改 门店表SystemStore nowMoney
		SystemStore updateSystemStore=new SystemStore();
		updateSystemStore.setNowMoney(storeNowMoney);
		updateSystemStore.setId(systemStore.getId());
		systemStoreService.update(updateSystemStore);
		
		Integer currentTime=(int) (new Date().getTime()/1000);
		systemStoreExtract.setAddTime(currentTime);
		systemStoreExtract.setStatus(0);
		//插入提现申请 eb_system_store_extract
		systemStoreExtractMapper.insert(systemStoreExtract);
		
		
		
		Integer primaryId=systemStoreExtract.getId();
		
		//插入账户流水 eb_system_store_bill
		/**保存余额*/
		SystemStoreBill target = new SystemStoreBill();
		target.setStoreId(storeId);
		//todo
		target.setLinkId(String.valueOf(primaryId));
		target.setPm("0");
		target.setTitle("余额提现");
		target.setCategory("now_money");
		target.setType("extract");
		target.setNumber(amount);
		target.setServiceCharge(serviceChargeDecimal);
		target.setMark("使用银联卡"+bankCode+"提现"+extractPrice+"元");
		//获取最后一条门店流水记录
		//减去的是扣除手续费后的  待确认  todo
		SystemStoreBill billTemp = systemStoreBillService.getLastTimeInfo(storeId);
		BigDecimal storeBillBalance=billTemp.getBalance();
		
//		Double totalBlance=storeBillBalance.doubleValue()-extractPrice;
		//余额减去提现金额
		Double totalBlance=ArithUtil.sub(storeBillBalance.doubleValue(),extractPrice);
		
		if(totalBlance==null || totalBlance<0){
			throw new ProductResException(ProductResExceptionCode.BALANCE_NOT_SUFFICIEN.getCode(),ProductResExceptionCode.BALANCE_NOT_SUFFICIEN.getMessage());
		}
		target.setStatus(1);
		target.setBalance(new BigDecimal(totalBlance).setScale(2,BigDecimal.ROUND_HALF_UP));
		target.setAddTime(DateUtil.getUTCTime(null).intValue());
		systemStoreBillService.save(target);
		
		//插入提现提醒 eb_store_reminder
		StoreReminder o = new StoreReminder();
		o.setReminderSn(StringUtils.getReminderSn());
		o.setReminderType("10");
		o.setStoreId(storeId);
		if(systemStore!=null){
			o.setStoreName(systemStore.getName());
		}
		o.setDataId(String.valueOf(primaryId));
		o.setTitle("提现申请");
		o.setMsg("<i style=\"color: red;\">提现"+amount+"元</i>");
		storeReminderService.save(o);
		
		WithdrawalApplicateVo withdrawalApplicateVo=new WithdrawalApplicateVo();
		withdrawalApplicateVo.setServiceCharge(String.valueOf(serviceChargePrice));
		withdrawalApplicateVo.setAccount(String.valueOf(amount));
		
		return withdrawalApplicateVo;
		
	}

	@Override
	public SystemStoreExtract queryByPrimaryId(Integer id) {
		SystemStoreExtract systemStoreExtract=systemStoreExtractMapper.queryByPrimaryId(id);
		return systemStoreExtract;
	}

	@Override
	public SystemStoreExtract queryLastStoreExtract(Integer storeId) {
		
		Map<String,Object> queryMap=new HashMap<String,Object>();
		queryMap.put("storeId", storeId);
		SystemStoreExtract systemStoreExtract=systemStoreExtractMapper.queryLastStoreExtract(queryMap);
		return systemStoreExtract;
	}


	
}